# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=libedit
pkgname=('libedit' 'libedit-devel')
_base_ver=3.1
_snap_date=20210216
pkgver=${_snap_date}_${_base_ver}
pkgrel=1
pkgdesc="Libedit is an autotool- and libtoolized port of the NetBSD Editline library."
groups=('libraries')
arch=('i686' 'x86_64')
license=('BSD')
url="https://www.thrysoee.dk/editline/"
depends=('ncurses' 'sh')
makedepends=('ncurses-devel')
options=('staticlibs')
source=(https://www.thrysoee.dk/editline/${pkgname}-${_snap_date}-${_base_ver}.tar.gz
        libedit-20191231-3.1.patch
        libedit-20130712-1.src.patch
        libedit-3.0-20120311-msys2.patch)
sha256sums=('2283f741d2aab935c8c52c04b57bf952d02c2c02e651172f8ac811f77b1fc77a'
            '858f123a0844bb16903014a9b3e7dd4668654931a07ca8a9a4d1a8a3572863b0'
            'e973c43b6576fbafef69f05c3c95ee256bfb972d3622e8c87658e737415cc258'
            '11161ee9688c2b8390809be057febf6ff33462b441b901f7a3150937ccbd7cd7')

prepare() {
  cd "${srcdir}/${pkgname}-${_snap_date}-${_base_ver}"

  patch -p1 -i ${srcdir}/libedit-20191231-3.1.patch
  patch -p2 -i ${srcdir}/libedit-20130712-1.src.patch
  patch -p1 -i ${srcdir}/libedit-3.0-20120311-msys2.patch
  autoreconf -fi
}

build() {
  cd "${srcdir}/${pkgname}-${_snap_date}-${_base_ver}"

  ./configure \
    --build=${CHOST} \
    --prefix=/usr \
    --enable-widec \
    CPPFLAGS="$CPPFLAGS -I/usr/include/ncursesw" \
    LDFLAGS="$LDFLAGS -L/usr/lib/ncursesw"

  make

  make DESTDIR=${srcdir}/dest install
  install -Dm644 COPYING ${srcdir}/dest//usr/share/licenses/libedit/LICENSE
}

package_libedit() {
  groups=('libraries')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
  rm "${pkgdir}"/usr/share/man/man3/history.3 # conflicts with readline
  cp "${pkgdir}"/usr/share/man/man3/editline.3 "${pkgdir}"/usr/share/man/man3/el.3
}

package_libedit-devel() {
  pkgdesc="libedit headers and libraries"
  groups=('development')
  depends=("libedit=${pkgver}" 'ncurses-devel')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}
